package com.tengju.bff.interfaces.manage.common.facade;

import com.tengju.bff.interfaces.manage.common.assembler.CommonManageAssembler;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.data.domain.model.analysis.MetaHolder;
import com.tengju.data.domain.model.report.AchievementBonusDateConfig;
import com.tengju.data.domain.model.service.AchievementBonusDateConfigService;
import com.tengju.data.domain.model.user.UserLevelEnum;
import com.tengju.data.domain.shared.DateUtil;
import com.tengju.data.domain.shared.Month;
import com.tengju.support.domain.model.article.ArticleType;
import com.tengju.support.domain.model.resource.Position;
import com.tengju.support.domain.model.resource.ResourceOrientation;
import com.tengju.user.domain.model.sign.CrmStarSignStatus;
import com.tengju.user.domain.model.sign.StarSignStatus;
import com.tengju.user.domain.model.user.StarLevelEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommonManageFacade {



    @Autowired
    private AchievementBonusDateConfigService achievementBonusDateConfigService;

    private final LocalDate CRM_STAR_DATE = LocalDate.of(2021, 1, 1);

    @Autowired
    private MetaHolder metaHolder;

    /**
     * {@link ArticleType}
     *
     * @return
     */
    public List<KVVO<String, Integer>> getArticleTypes() {
        return CommonManageAssembler.getArticleTypes();
    }

    /**
     * {@link Position}
     *
     * @return
     */
    public List<KVVO<String, Integer>> getResourcePositions() {
        return CommonManageAssembler.getResourcePositions();
    }

    /**
     * {@link ResourceOrientation}
     *
     * @return
     */
    public List<KVVO<String, Integer>> getResourceOrientations() {
        return CommonManageAssembler.getResourceOrientations();
    }

    /**
     * {@link StarLevelEnum}
     *
     * @return
     */
    public List<KVVO<String, Integer>> getStarLevels() {
        return CommonManageAssembler.getStarLevels();
    }

    /**
     * {@link StarLevelEnum}
     *
     * @return
     */
    public List<KVVO<String, Integer>> getStarLevelsShortName() {
        return CommonManageAssembler.getStarLevelsShortName();
    }

    /**
     * {@link StarSignStatus}
     *
     * @return
     */
    public List<KVVO<String, Integer>> getStarSignStatus() {
        return CommonManageAssembler.getStarSignStatus();
    }

    /**
     * {@link CrmStarSignStatus}
     *
     * @return
     */
    public List<KVVO<String, Integer>> getCrmStarSignStatus() {
        return CommonManageAssembler.getCrmStarSignStatus();
    }

    public List<String> getQuarterList() {
        return DateUtil.getQuarterList();
    }

    public List<KVVO> getMonthList() {
        LocalDate now = LocalDate.now();
        List<Month> subDateMonths = DateUtil.subDateMonth(CRM_STAR_DATE, now);
        if (CollectionUtils.isEmpty(subDateMonths)) {
            return Collections.emptyList();
        }
        return subDateMonths.stream()
                .map(v -> KVVO.of(v.format("yyyy年MM月"), v.getMonthInt()))
                .collect(Collectors.toList());
    }

    public List<KVVO> getMonthList(LocalDate starDate) {
        LocalDate now = LocalDate.now();
        List<Month> subDateMonths = DateUtil.subDateMonth(starDate, now);
        if (CollectionUtils.isEmpty(subDateMonths)) {
            return Collections.emptyList();
        }
        return subDateMonths.stream()
                .map(v -> KVVO.of(v.format("yyyy年MM月"), v.getMonthInt()))
                .collect(Collectors.toList());
    }


    /**
     * {@link UserLevelEnum}
     *
     * @return
     */
    public List<KVVO<String, Integer>> getUserLevels() {
        return CommonManageAssembler.getUserLevels();
    }

    public List<KVVO<String, Integer>> getDateTypeList() {
        return CommonManageAssembler.getDateTypeList();
    }


    public List<KVVO> getBonusMonthList() {
        LocalDate now = LocalDate.now();
        AchievementBonusDateConfig dateConfig = achievementBonusDateConfigService.queryByCalDateOrDefault(now);
        LocalDate calDate = dateConfig.getEndDate() == null ? now : dateConfig.getEndDate();
        List<Month> subDateMonths = DateUtil.subDateMonth(CRM_STAR_DATE, calDate);
        if (CollectionUtils.isEmpty(subDateMonths)) {
            return Collections.emptyList();
        }
        return subDateMonths.stream()
                .map(v -> KVVO.of(v.format("yyyy年MM月"), v.getMonthInt()))
                .collect(Collectors.toList());
    }

    public List<KVVO> listMetas(String metaGroupName) {
        return metaHolder.listMeta(metaGroupName).values().stream()
                .map(item -> {
                    return KVVO.of(item.getShowName(),item.getColName());
                })
                .collect(Collectors.toList());
    }
}
